package quickstart.action;

import quickstart.model.User;
import quickstart.service.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class UserAction extends ActionSupport implements Preparable, ModelDriven<User>{
    private static final long serialVersionUID = 1L;
    private UserService userService;
    private User user = new User();;
    private Integer id;


    public UserService getUserService() {
        return userService;
    }


    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    public User getUser() {
        return user;
    }


    public void setUser(User user) {
        this.user = user;
    }


    public UserAction (UserService service) {
        this.userService = service;
    }


    public String register() {
        this.userService.register(user);
        this.user = new User();
        return Action.SUCCESS;
    }
    
    public String update() {
        this.userService.update(user);
        return Action.SUCCESS;
    }
    

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void prepare() throws Exception {
        if (id != null)
            user = userService.find(id);
    }


    public User getModel() {
        return this.user;
    }

    @Override
    public void validate()
    {
        
    }

}
